import { _decorator, Component, Texture2D, SpriteFrame, SubContextView, Node, macro, UITransform } from "cc";


let DomainAction = {
    FetchFriend: "FetchFriend",
    FetchFriendScore: "FetchFriendScore", //好友挑战排行
    FetchFriendPassScore: "FetchFriendPassScore", //好友打卡排行
    HorConmpar: "HorConmpar", //横向比较 horizontal comparison
    MoreFriend: "MoreFriend",
    SelfIcon: 'SelfIcon',//玩家头像
    PlayerIcon: 'PlayerIcon',//个人头像
    Scrolling: "Scrolling"
}

const { ccclass, property } = _decorator;

@ccclass
export default class WXRankList extends Component {

    static instance: WXRankList = null;

    @property(Node)
    rankRender: Node = null; // render spr
    @property(Node)
    touchLayer: Node = null;
    @property(Node)
    move: Node = null;
    // @property(Boolean)
    enableScroll = true;//

    rankTexture: Texture2D = null;
    rankSpriteFrame: SpriteFrame = null;
    update_time = 0;
    is_close: boolean = true;
    is_moreRank: boolean = false;
    userInfo = null;

    onLoad() {
        WXRankList.instance = this;
        this.is_close = true;
        this.init();
    }

    init() {
        this.rankTexture = new Texture2D();
        this.rankSpriteFrame = new SpriteFrame();
        this.resizeSharedCanvas(this.rankRender.getComponent(UITransform).width, this.rankRender.getComponent(UITransform).height);
    }



    onEnable() {
        // this.touchLayer.active = true
        // if (this.enableScroll) {
        //     this.rankRender.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        // }

        // this.postMessage(Consts.DomainAction.FetchFriendLevel)
    }

    onDestroy() {

    }

    resizeSharedCanvas(width, height) {
        if (!window["wx"]) return;
        let sharedCanvas = window["wx"].getOpenDataContext().canvas
        sharedCanvas.width = width
        sharedCanvas.height = height
    }

    changeRender(renderNode: Node) {
        if (renderNode.name === "moreRender") {
            this.is_moreRank = true;
        } else if (renderNode.name === "rankListRender") {
            this.is_moreRank = false;
        }
        this.rankRender.getComponent(UITransform).width = renderNode.getComponent(UITransform).width
        this.rankRender.getComponent(UITransform).height = renderNode.getComponent(UITransform).height
        this.rankRender.position = renderNode.position
        this.resizeSharedCanvas(renderNode.getComponent(UITransform).width, renderNode.getComponent(UITransform).height)
    }

    onTouchMove(event) {
        const deltaY = event.getDeltaY();
        // console.log("rank touchmove:", deltaY);
        this.postMessage("Scrolling", deltaY)
    }

    close() {
        this.is_close = true;
        // this.rankSpriteFrame.setTexture(null);
        this.resizeSharedCanvas(this.rankRender.getComponent(UITransform).width, this.rankRender.getComponent(UITransform).height);
        this.rankRender.getComponent(SubContextView).enabled = false;
        this.rankRender.active = false;
        // this.touchLayer.active = false;
        this.move.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
    }

    onPageUp() {
        this.update_time = 0;
        this.postMessage("Paging", -1)
    }

    onPageDown() {
        this.update_time = 0;
        this.postMessage("Paging", 1)
    }

    //分数排行
    loadScoreRank() {
        console.log('分数排行')
        this.rankRender.getComponent(SubContextView).enabled = true;
        if (this.enableScroll) {
            this.move.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        }
        this.postMessage(DomainAction.FetchFriendScore)
        this.update_time = 0;
        this.rankRender.active = true;
        this.rankRender.setPosition(0, 0);
        this.touchLayer.active = true
    }

    // //获取关卡得分排行
    // loadLevelScoreRank(level){
    //     this.rankRender.active = true;
    //     this.update_time = 0;
    //     this.touchLayer.active = true
    //     this.onEnable()
    //     this.changeRender(this.rankListNode)        
    //     this.postMessage(DomainAction.FetchFriendScore, level)
    // }

    //超越好友
    moreThenFriend() {
        this.rankRender.getComponent(SubContextView).enabled = true;
        this.postMessage(DomainAction.MoreFriend, 1);
        // this.rankRender.parent = this.thenParent;
        // let posWorld = this.thenParent.convertToWorldSpaceAR(v2(0, 0));
        // let pos = this.rankRender.convertToWorldSpaceAR(v2(this.rankRender.width / 2 , -this.rankRender.height / 2));
        // console.info('posWorld', posWorld.x, posWorld.y);
        // console.info('pos', pos.x, pos.y);
        // let x = posWorld.x - pos.x; 
        // let y = posWorld.y - pos.y; 
        // console.info(x, y);
        this.rankRender.setPosition(534, 170);
        this.rankRender.active = true;
    }

    //玩家头像及名字
    iconName() {
        // this.quiteMessage();
        // this.is_close = false;
        // let icon_point = Game.instance.homeUI.icon.position;
        // let point = this.rankRender.convertToNodeSpaceAR(icon_point);
        // this.postMessage(DomainAction.SelfIcon,this.userInfo,point.y);
        // this.update_time = 0;
        // this.rankRender.active = true;
    }
    //个人头像及名字
    playerName() {
        // this.quiteMessage();
        // this.is_close = false;
        // let icon_point = Game.instance.homeUI.playericon.position;
        // let point = this.rankRender.convertToNodeSpaceAR(icon_point);
        // this.postMessage(DomainAction.PlayerIcon,this.userInfo,point.y);
        // this.update_time = 0;
        // // Game.instance.homeUI.playericon.getComponent(Sprite).spriteFrame = null;
        // this.rankRender.active = true;
    }

    //向子域发送消息
    postMessage(action, data = null, dataEx = null) {
        if (!window["wx"]) return;

        let openDataContext = window["wx"].getOpenDataContext();
        openDataContext.postMessage({
            action: action,
            data: data,
            dataEx: dataEx,
        })
    }

    //向微信请求数据
    quiteMessage() {
        if (!window["wx"]) return;
        window["wx"].getUserInfo({
            openIdList: ["selfOpenId"],
            success: res => {
                console.log (res)
                this.userInfo = res.data[0];
            }
        })
    }



    //删除微信数据
    removeUserKey(key_or_keys) {
        if (!window.window["wx"]) return
        if (typeof (key_or_keys) === "string") {
            key_or_keys = [key_or_keys]
        }
        window["wx"].removeUserCloudStorage({
            keyList: key_or_keys,
            success: (res) => {
            },
            fail: (res) => {
            }
        })
    }

}
